import { getLayoutProps } from "@/common/layout/getProps";
import { ProForm } from "@/components/ProForm";
import { data } from "@/pages/performanceStatisticalReports/data";
import { PlusOutlined } from "@ant-design/icons";
import { PageContainer } from "@ant-design/pro-layout";
import {
  Button, DatePicker,
  Divider,
  Form,
  Input,
  Modal,
  Row,
  Select,
  Table, 
  message,
  Pagination,
  type TableColumnsType,
  type TableProps
} from "antd";
import dayjs from "dayjs";
import { useRouter } from "next/navigation";
import { type Key, useState,useEffect } from "react";
import { http } from "@/utils/request";
import axios from 'axios';

type TableDataType = Record<string, any>;

type TableRowSelection<T> = TableProps<T>["rowSelection"];

const { confirm } = Modal;
const Index = () => {
  const router = useRouter();
  const [form] = Form.useForm()
  const [list, setList] = useState([]);
  const [pageNum,setPageNum] = useState(1);
  const [pageSize,setPageSize] = useState(10);
  const [total,setTotal] = useState();
  const [activityOption,setActivityOption] = useState([]);

  const columns: TableColumnsType = [
    {
      align: "center",
      title: "序号",
      width: 60,
      key: "index",
      render: (_text, _record, index) => {
        return index + 1
      }
    },
    {
      align: "center",
      title: "评优活动",
      dataIndex: "activityName"
    },
    // {
    //   align: "center",
    //   title: "投票人",
    //   dataIndex: "timeRange"
    // },
    // {
    //   align: "center",
    //   title: "所属部门",
    //   dataIndex: "num2"
    // },
    {
      align: "center",
      title: "创建人",
      dataIndex: "createdBy"
    },
    {
      align: "center",
      title: "创建时间",
      dataIndex: "createdTime"
    },
    {
      align: "center",
      title: "申请状态",
      dataIndex: "auditStatus",
      width:120,
      fixed:'right',
      render: (_, record) => {
        return (
          <>
            {record.auditStatus === "1" && <span>待提交</span>}
            {record.auditStatus === "2" && <span>审核中</span>}
            {record.auditStatus === "3" && <span>已完成</span>}
          </>
        )
      }
    },
    {
      align: "center",
      title: "操作",
      render: (_, record, index) => {
        return (
          <>
            <Button type={'link'} onClick={() => {
              router.push(`/employeeExcellentEvaluation/voterConfirm/voterConfirmDetail?id=${record.id}&type=detail`)
            }}>详情</Button>
            <Button type={'link'} onClick={() => {
              router.push(`/employeeExcellentEvaluation/voterConfirm/voterConfirmEdit?id=${record.id}`)
            }}>编辑</Button>
            <Button type={'link'} danger onClick={() => {
               confirm({
                title: '提示?',
                content: '此操作将永久删除该数据，是否继续?',
                cancelText:'取消',
                okText:'确定',
                onOk() {
                  delData(record.id)
                },
                onCancel() {
                  console.log('Cancel');
                },
              });
            }}>删除</Button>
          </>
        )
      }
    }
  ]
  //修改页码或分页大小
  const changePagination = async (page: number, size: number)=>{
    setPageNum(page);
    if(size!==pageSize){
      setPageNum(1);
    }
    setPageSize(size);
  }
  //删除
  const delData = async(id:number) =>{
    try {
      const res = await http.get({
        url:'/PmVotePersonReq/remove',
        params:{
          deleteId:id,
        }
      })
      if(res.code==200){
        message.success('删除成功!');
        getList();
      }else{
        message.error(res.msg);
      }
    } catch (errorInfo) {
      console.log(errorInfo);
    }
  }
  //查询接口
  const getList = async() =>{
    try {
      const res = await http.get({
        url:'/PmVotePersonReq/qryList',
        params:{
          activityId:form.getFieldValue('activityId'),
          createdBy:form.getFieldValue('createdBy'),
          auditStatus:form.getFieldValue('auditStatus'),
          createdTime:form.getFieldValue('createdTime')?dayjs(form.getFieldValue('createdTime')).format('YYYY-MM-DD'):undefined,
          pageNum:pageNum,
          pageSize:pageSize,
        }
      })
      setList(res.rows);
      setTotal(res.total);
    } catch (errorInfo) {
      console.log(errorInfo);
    }
  }
  const getActivity = async() =>{
    try {
      const res = await http.get({
        url:'/PmExcellentActivity/queryPmExcellentActivityAll',
      })
      
      setActivityOption(res.rows.map(item=>({value:item.id,label:item.activityName})));
    } catch (errorInfo) {
      console.log(errorInfo);
    }
  }
  useEffect(()=>{
    getList();
    getActivity();
  },[pageNum,pageSize])

  return (
    <PageContainer>
      <ProForm form={form} layout={'inline'} onSearch={getList} onReset={getList}>
        <Form.Item
          label={'申请人'}
          name="createdBy"
        >
          <Input placeholder={'请输入'} />
        </Form.Item>
        <Form.Item
          label={'评优活动'}
          name="activityId"
        >
          <Select
            placeholder='请选择'
            options={activityOption}>
          </Select>
        </Form.Item>
        {/* <Form.Item
          label={'申请部门'}
          name="field1"
        >
          <Select
            options={[
              { value: 'option1', label: 'option1' },
              { value: 'option2', label: 'option2' },
              { value: 'option3', label: 'option3' },
            ]}
          ></Select>
        </Form.Item> */}
        <Form.Item
          label={'流程状态'}
          name="auditStatus"
        >
          <Select
            placeholder='全部'
            options={[
              { value: '1', label: '待提交' },
              { value: '2', label: '审核中' },
              { value: '3', label: '已完成' },
            ]}
          ></Select>
        </Form.Item>
        <Form.Item
          label={'申请日期'}
          name="createdTime"
        >
          <DatePicker />
        </Form.Item>
      </ProForm>
      <Divider className={'mb-[20px] mt-0'} />

      <Row className={'mb-[20px]'}>
        <Button type={'primary'} icon={<PlusOutlined />} onClick={() => {
          router.push('/employeeExcellentEvaluation/voterConfirm/voterConfirmCreate')
          }}>新增</Button>
      </Row>

      <Table
        dataSource={list}
        columns={columns}
        rowKey={"id"}
        bordered
        pagination={false}
      ></Table>
      <Pagination
        className={'m-[20px] text-end'}
        current={pageNum}
        pageSize={pageSize}
        total={total}
        showSizeChanger
        showQuickJumper
        showTotal={(total) => `共 ${total} 条`}
        onChange={changePagination}
      />
    </PageContainer>
  )
}

export const getServerSideProps = getLayoutProps;
export default Index;
